
#include "registerpage.h"
#include "registerpage_p.h"
#include "chttpsupport.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QSvgRenderer>
#include <QLineEdit>
#include <QPushButton>
#include <QJsonObject>
#include <QPainter>
#include <QJsonDocument>
#include <QAction>

RegisterPagePrivate::RegisterPagePrivate(RegisterPage *q)
    : q_ptr(q)
{
    initUi();
}

RegisterPagePrivate::~RegisterPagePrivate()
{

}

void RegisterPagePrivate::initUi()
{
    QWidget *widget = new QWidget(q_ptr);
    widget->setObjectName("register_widget");
    auto *vLayout = new QVBoxLayout(q_ptr);
    vLayout->setMargin(0);
    vLayout->addWidget(widget);
    QGridLayout *gLayout = new QGridLayout(widget);
    int index = 0;
    auto *label1 = new QLabel(QObject::tr("用户"), widget);
    line_user_ = new QLineEdit(widget);
    line_user_->setObjectName("line_login");
    gLayout->addWidget(label1, index, 0);
    gLayout->addWidget(line_user_, index, 1, 1, 2);
    index++;
    auto *label2 = new QLabel(QObject::tr("邮箱"), widget);
    line_email_ = new QLineEdit(widget);
    line_email_->setObjectName("line_login");
    gLayout->addWidget(label2, index, 0);
    gLayout->addWidget(line_email_, index, 1, 1, 2);
    index++;

    // 加载 SVG 文件
    QSvgRenderer svgRenderer(QStringLiteral(":/resources/svg/show.svg"));
    // 创建一个 QPixmap，大小与 SVG 默认大小匹配
    QPixmap pixmap(25, 25);
    pixmap.fill(Qt::transparent);  // 设置背景透明
    QPainter painter(&pixmap);
    svgRenderer.render(&painter);

    auto *label3 = new QLabel(QObject::tr("密码"), widget);
    line_passwd_ = new QLineEdit(widget);
    line_passwd_->setObjectName("line_login");
    QAction *seePasswdAction = new QAction(QIcon(pixmap), QObject::tr("展示密码"), widget);
    line_passwd_->addAction(seePasswdAction, QLineEdit::TrailingPosition);
    gLayout->addWidget(label3, index, 0);
    gLayout->addWidget(line_passwd_, index, 1, 1, 2);
    index++;
    auto *label4 = new QLabel(QObject::tr("确认密码"), widget);
    line_repeat_ = new QLineEdit(widget);
    line_repeat_->setObjectName("line_login");
    gLayout->addWidget(label4, index, 0);
    gLayout->addWidget(line_repeat_, index, 1, 1, 2);
    index++;
    auto *label5 = new QLabel(QObject::tr("验证码"), widget);
    line_code_ = new QLineEdit(widget);
    line_code_->setObjectName("line_login");
    btn_code_ = new QPushButton(QObject::tr("获取"), widget);
    gLayout->addWidget(label5, index, 0);
    gLayout->addWidget(line_code_, index, 1);
    gLayout->addWidget(btn_code_, index, 2);
    index++;
    auto *btn1 = new QPushButton(QObject::tr("确认"), widget);
    auto *btn2 = new QPushButton(QObject::tr("取消"), widget);
    QHBoxLayout *layout = new QHBoxLayout();
    layout->addWidget(btn1);
    layout->addWidget(btn2);
    gLayout->addItem(layout, index, 0, 1, 3);

    QObject::connect(btn2, &QPushButton::clicked, q_ptr, [this]{
        emit q_ptr->sig_login();
    });
}

RegisterPage::RegisterPage(QWidget *parent)
    : QWidget(parent)
    , d_ptr(new RegisterPagePrivate(this))
{
    connect(d_ptr->btn_code_, &QPushButton::clicked, this, &RegisterPage::btnCodeClicked);
    connect(&CHttpSupport::instance(), &CHttpSupport::httpPostRspReady, this, &RegisterPage::postBack);
}

RegisterPage::~RegisterPage()
{
    if (d_ptr)
    {
        delete d_ptr;
        d_ptr = nullptr;
    }
}

void RegisterPage::btnCodeClicked()
{
    QJsonObject obj;
    obj["email"] = "1129216137@qq.com";

    CHttpSupport::instance().httpPost("http://127.0.0.1:8080/post_test", QJsonDocument(obj).toJson());
}

void RegisterPage::postBack(QString url, QString postMD5, QByteArray data)
{
    qDebug() << QJsonDocument::fromJson(data).object();
}
